<?php
/**
* @file Invitecodem.php
* @synopsis  邀请码管理
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015年8月15日14:58:48
*/


class Invitecodem extends CI_Model
{
	function __construct()
	{
	}

	public function insert($insert_string)
	{
		$result = $this->db->query($insert_string);

		return $result ? true : false;
	}


	//获取邀请码
	public function get_one_invitecode($uid)
	{
		//如果uid为空  返回空字符串
		if(empty($uid))
		{
			return '';
		}
		$mc = $this->cache->memcached;
		$key = 'invite_code'.$uid;
		$invite_code = $mc->get($key);
		if(!$invite_code)
		{
			$invite_code = $this->db->where(array("id"=>$uid))->get("ml_invite_code")->result_array();
			$invite_code = $invite_code[0]["invite_code"];
			$mc->save($key,$invite_code, 60*3);
		}
		//获取中间值的邀请码

		return $invite_code;
		
	}

	//根据邀请码获取邀请人
	public function get_invite_code_owned($invite_code)
	{
		//获取中间值的邀请码
		$invite_code = $this->db->select()->where(array("invite_code"=>$invite_code))->get("ml_invite_code")->result_array();
		return $invite_code[0]["id"];
		
	}

	//获取一个用户一段时间内的邀请人数
	public function get_invite_count($uid,$start_time,$end_time)
	{
		$where = [];
		$where['b.id'] = $uid;
		$where['a.add_time >= '] = $start_time;
		$where['a.add_time < '] = $end_time;
		$invite_count = $this->db->select('count(a.id) as sum')
			->from('ml_fellow as a')
			->join('ml_invite_code as b','a.invite_code=b.invite_code','left')
			->where($where)
			->where_in('a.verified_status',array(3,5))
			->get()->row_array();
		//echo 'invite_sql is '.$this->db->last_query().'<br /><br />';
		return $invite_count['sum'];
	}
}
?>
